// pages/login/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:''
  },
  //绑定手机号动态
  getPhone(e){
    this.setData({
      phone:e.detail.value,
      initCountDown:app.globalData.initCountDown,//原始倒计时
      countDown:app.globalData.initCountDown,//倒计时
    })
  },
   //验证阿妈发送中
   onCodeDown(){
    let that = this;
    let time = that.data.countDown;
    let setTime;
    clearInterval(setTime)
    time --;
    that.setData({
      countDown:time
    })
    setTime = setInterval(()=>{
      time --;
      console.log
      if(time<=0){
        that.setData({
          countDown:that.data.initCountDown
        })
        clearInterval(setTime);
      }else {
        that.setData({
          countDown:time
        })
      }
    },1000)
  },
   //获取验证码
   onGetCode(){
    let phone = this.data.phone;
    let that = this;
    if(!phone || phone.length<=0){
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }

    if (!(/^1[345789]\d{9}$/.test(phone))) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    this.onCodeDown()
    wx.request({
      url: app.globalData.baseUrl + '/api/getVerifyCode',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': app.globalData.token
      },
      data: {
        "phone": phone,
      },
      success: function(res) {
        if(res.data.status){

        }

      },
      fail: function() {

        console.log("请求失败")
      }
    })
  },
  onSubmit(e){
      let phone = e.detail.value.phone;
      let verify_code = e.detail.value.verify_code;
      let password = e.detail.value.password;
      let password_confirmation = e.detail.value.password_confirmation;
      let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
      if(!phone || phone.length<=0){
        wx.showToast({
          title: "请输入手机号",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if(!verify_code || verify_code.length<=0){
        wx.showToast({
          title: "请输入验证码",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if(!password || password.length<=0){
        wx.showToast({
          title: "请输入密码",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if(!password_confirmation || password_confirmation.length<=0){
        wx.showToast({
          title: "请输入确认密码",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if (!(/^1[345789]\d{9}$/.test(phone))) {
        wx.showToast({
          title: "请输入正确的手机号",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if(!(reg.test(password))){
        wx.showToast({
          title: "密码必须包含数字和字符且8-16位",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if(password_confirmation != password){
        wx.showToast({
          title: "密码不一致",
          icon: 'none',
          duration: 2000
        })
        return false
      }
      wx.showLoading({
        title: '提交中',
        mask:true
      })

      wx.request({
        url: app.globalData.baseUrl + '/api/register/forgetPassword',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          "phone": phone,
          "verify_code": verify_code,
          "password":password,
          "password_confirmation":password_confirmation
        },
        success: function(res) {
          wx.hideLoading()
         if(res.data.status){
          wx.navigateTo({
            url: '/pages/login/index',
          })
         }else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
         }
        },
        fail: function() {
          wx.hideLoading()
          console.log("请求失败")
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
